package org.csu.jpetstoremanagement.service;

import org.csu.jpetstoremanagement.domain.Log;
import org.csu.jpetstoremanagement.persistence.LogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LogService {

    @Autowired
    private LogMapper logMapper;

    public List<Log> getAllLog() {
        return logMapper.getAllLog();
    }

    public void deleteLog(String date) {
        logMapper.deleteLog(date);
    }

    public Log getLogByDate(String username) {
        return logMapper.getLogByDate(username);
    }

    public void info(String username, String ip, String operation) {
        info(username, ip, operation, null);
    }

    public void info(String username, String ip, String operation, String remark) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("ip", ip);
        map.put("type", "INFO");
        map.put("operation", operation);
        map.put("remark", remark);
        logMapper.insertLog(map);
    }

    public void error(String username, String ip, String operation) {
        error(username, ip, operation, null);
    }

    public void error(String username, String ip, String operation, String remark) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("ip", ip);
        map.put("type", "ERROR");
        map.put("operation", operation);
        map.put("remark", remark);
        logMapper.insertLog(map);
    }

    public int getViewNumber(){
        return logMapper.getViewNumber();
    }

    public int getTodayViewNumber(String day){
        return logMapper.getTodayViewNumber(day);
    }

}
